Introduction

After covering directives, program development tools, and Input/Output in assembly language programming, let's delve deeper into assembly language programs. This unit will begin by exploring simple assembly programs, which handle basic tasks such as data transfer, arithmetic operations, and shifts. A fundamental example includes determining the larger of two numbers. Subsequently, we'll progress to more intricate programs demonstrating the use of loops and various comparisons to accomplish tasks such as code conversion, character coding, and finding the largest value in an array. Additionally, this unit delves into more advanced arithmetic and string operations, as well as modular programming. For further details on these programming concepts, additional readings are recommended.

Simple Assembly Programs

The 0–1 combinations that the computer decodes directly make up machine language code. However, the following issues with the machine language exist:

  • Writing in 0-1 forms is challenging for most individuals and heavily relies on the computer.
  • Debugging is challenging.
  • The machine code is incredibly hard to decipher. As a result, it will be difficult to understand program logic.

Computer manufacturers have created English-like terms to describe a machine's binary instruction in order to get around these problems. A mnemonic is a symbolic code that corresponds to each instruction. A specific instruction's mnemonic is made up of letters that allude to the task that the instruction is supposed to complete. The ADD mnemonic, for instance, is used to add two numbers. Machine language instructions can be written in symbolic form using these mnemonics, with one corresponding symbolic instruction for each machine instruction. We refer to this as an assembly language.

Advantages and Disadvantages

Advantages

🔧

Hardware Control

Assembly language provides extensive control over specific hardware and software components, enabling in-depth exploration of instruction sets, addressing modes, interrupts, and more.

🚀

Compact Executables

Programs written in assembly generate smaller, more compact executable modules due to their close proximity to the machine. This proximity allows for the creation of highly optimized programs, resulting in faster execution.

📦

Code Density

Assembly language programs tend to be at least 30% denser than equivalent programs written in high-level languages. This density arises from the fact that compilers often produce lengthy code sequences for each instruction, whereas assembly language typically employs a single line of code for each instruction, particularly noticeable in string-related programs.

🔓

Programming Freedom

Assembly language offers programmers significant freedom, as it imposes few restrictions or rules, allowing for flexible system construction.

Disadvantages

🖥️

Machine Dependency

Assembly language is inherently machine-dependent, with each microprocessor featuring its own unique set of instructions. Consequently, assembly programs lack portability.

🧠

Complexity

Programming in assembly requires a deep understanding of underlying hardware architecture, making it more complex and time-consuming compared to high-level languages.

🐛

Debugging Challenges

Debugging and maintaining assembly code can be challenging due to its intricate and less readable nature compared to code written in higher-level languages.

⏱️

Development Effort

Development in assembly language often demands more effort and expertise, potentially increasing development time and costs, and making it less accessible to beginners.

What the Assembler Does

A source program is required before using the assembler. Instructions written in assembly language by the programmer make up the source program. These instructions have been written with mnemonic labels and opcodes. Programs written in assembly language and sent to the assembler must be machine-readable. You can keep source programs as paper tape or diskette files with the help of the text editor included in the Intellect development system. After that, you can give the assembler the generated source program file. The 8080 and 8085 microprocessors can run object code thanks to the assembler program, which handles the tedious operation of converting symbolic code.


The output of the assembler can be found in three different files: the object file, which is your program translated into object code; the list file, which is a printout of your source code, the object code produced by the assembler, and the symbol table; and the symbol-cross reference file, which is a list of such records.

Function of an Assembler

Source Program
(Assembly Language)
⬇️
Assembler
⬇️
Object File
(Machine Code)
⬇️
Executable Program

Example Programs

💾Example 1: Store Data Byte

Statement: Store the data byte 32H into memory location 4000H.

Program 1

1
MVI A, 32H; Store 32H in the accumulator
2
STA 4000H; Copy accumulator contents at address 4000H
3
HLT; Terminate program execution

Program 2

1
LXI H, 4000H; Load HL with 4000H
2
MVI M, 32H; Store 32H in memory location pointed by HL register pair (4000H)
3
HLT; Terminate program execution

🔄Example 2: Exchange Memory Contents

Statement: Exchange the contents of memory locations 2000H and 4000H.

Program 1

1
LDA 2000H; Get the contents of memory location 2000H into accumulator
2
MOV B, A; Save the contents into B register
3
LDA 4000H; Get the contents of memory location 4000H into accumulator
4
STA 2000H; Store the contents of accumulator at address 2000H
5
MOV A, B; Get the saved contents back into A register
6
STA 4000H; Store the contents of accumulator at address 4000H
7
HLT; Terminate program execution

Program 2

1
LXI H, 2000H; Initialize HL register pair as a pointer to memory location 2000H
2
LXI D, 4000H; Initialize DE register pair as a pointer to memory location 4000H
3
MOV B, M; Get the contents of memory location 2000H into B register
4
LDAX D; Get the contents of memory location 4000H into A register
5
MOV M, A; Store the contents of A register into memory location 2000H
6
MOV A, B; Copy the contents of B register into accumulator
7
STAX D; Store the contents of A register into memory location 4000H
8
HLT; Terminate program execution

Example 3: Add Two 8-bit Numbers

Statement: Add the contents of memory location 4000H and 4001H and store the result in 4002H.

Sample problem

(4000H) = 14H
(4001H) = 89H
Result = 14H + 89H = 9DH

Source program

1
LXI H, 4000H; HL points 4000H
2
MOV A, M; Get first operand
3
INX H; HL points 4001H
4
ADD M; Add second operand
5
INX H; HL points 4002H
6
MOV M, A; Store result at 4002H
7
HLT; Terminate program execution

Example 4: Subtract Two 8-bit Numbers

Statement: Subtract the contents of memory location 4001H from the memory location 2000H and place the result in memory location 4002H.

Sample problem

(4000H) = 51H
(4001H) = 19H
Result = 51H - 19H = 38H

Source program

1
LXI H, 4000H; HL points 4000H
2
MOV A, M; Get first operand
3
INX H; HL points 4001H
4
SUB M; Subtract second operand
5
INX H; HL points 4002H
6
MOV M, A; Store result at 4002H
7
HLT; Terminate program execution